﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

public class Helper
{
	/// <summary>
	/// 对字符串进行加密。
	/// </summary>
	/// <param name="src">要加密的字符串。</param>
	/// <returns>返回加密后的字符串。</returns>
	public static string Encrypt(string src)
	{
		if (src == "") return src;

		Encoding enc = Encoding.Unicode;
		byte[] input = enc.GetBytes(src);
		byte[] output = Helper.Encrypt(input);

		string result = System.Convert.ToBase64String(output);
		return result;
	}

	/// <summary>
	/// 对字节数组进行加密。
	/// </summary>
	/// <param name="input">要加密的字节数组。</param>
	/// <returns>返回加密后的字节数组。</returns>
	public static byte[] Encrypt(byte[] input)
	{
		byte[] key = { 0x19, 0x76, 0x03, 0x08, 0x00, 0x15, 0x19, 0x80 };
		byte[] iv = { 0x08, 0x03, 0x44, 0x39, 0x13, 0x18, 0x99, 0x33 };

		DESCryptoServiceProvider desp = new DESCryptoServiceProvider();
		ICryptoTransform enf = desp.CreateEncryptor(key, iv);

		byte[] output = enf.TransformFinalBlock(input, 0, input.Length);

		return output;
	}

	/// <summary>
	/// 对字节数组进行解密。
	/// </summary>
	/// <param name="input">要解密的字节数组。</param>
	/// <returns>返回解密后的字节数组。</returns>
	public static byte[] Decrypt(byte[] input)
	{
		byte[] key = { 0x19, 0x76, 0x03, 0x08, 0x00, 0x15, 0x19, 0x80 };
		byte[] iv = { 0x08, 0x03, 0x44, 0x39, 0x13, 0x18, 0x99, 0x33 };

		DESCryptoServiceProvider desp = new DESCryptoServiceProvider();
		ICryptoTransform enf = desp.CreateDecryptor(key, iv);

		byte[] output = enf.TransformFinalBlock(input, 0, input.Length);

		return output;
	}

	/// <summary>
	/// 对字符串进行解密。
	/// </summary>
	/// <param name="src">要解密的字符串。</param>
	/// <returns>返回解密后的字符串。</returns>
	public static string Decrypt(string src)
	{
		if (src == "") return src;

		Encoding enc = Encoding.Unicode;

		byte[] input = System.Convert.FromBase64String(src);
		byte[] output = Helper.Decrypt(input);

		string result = enc.GetString(output);
		return result;
	}
}
